import Vue from 'vue'
import Vuex from 'vuex'
import createPersistedState from "vuex-persistedstate"
Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    image: [],
    history: [],
    BookList: [],
    Diamonds: 0,
    ids: 0,
    hist: [],
    jg: 0
  },
  mutations: {
    jsjg(state, Diamonds) {
      state.jg += Diamonds
      console.log(Diamonds);
    },
    delDatas(state, app) {
      let sx = state.BookList
      sx.findIndex((item, index) => {
        if (item.id == app) {
          state.BookList.splice(index, 1)
          return
        }
      })
    },
    addData(state, app) {
      let sx = state.image
      sx.findIndex(item => {
        if (item.id == app) {
          state.BookList.push(item)
          return
        }
      })
    },
    delData(state) {
      state.hist = []
    },
    histdel(state, index) {
      state.hist.splice(index, 1)
    },
    hists(state, obj) {
      state.hist.push(obj)
      console.log(state.hist);
    },
    ModifyID(state, obj) {
      state.ids = obj
    },
    imageData(state, image) {
      let add = state.image
      let index = add.findIndex(item => item.id == image.id)
      if (index == -1) {
        state.image.push(image)
      }
    },
    add(state, index) {
      state.image[index].flag = !state.image[index].flag
      state.BookList.push(state.image[index])
    },
    del(state, index) {
      state.image[index].flag = !state.image[index].flag
      state.BookList.splice(index, 1)
    },
    DeleteData(state) {
      let BookList = state.BookList
      for (let i = 0; i < BookList.length; i++) {
        if (BookList[i].flass) {
          BookList[i].flass = !BookList[i].flass
          state.image[i].flag = !state.image[i].flag
          state.BookList.splice(i, 1)
          i--
        }
      }
    },
    SelectAll(state) {
      let image = state.image
      for (let i = 0; i < image.length; i++) {
        if (!image[i].imags) {
          image[i].imags = true
        } else {
          image[i].imags = false
        }
      }
    },
    flags(state, index) {
      console.log(index);
      state.BookList[index].flass = !state.BookList[index].flass
    }
  },
  actions: {},
  modules: {},
  plugins: [createPersistedState()]
})